home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
print
/
gs261sr1.zip
/
GP_MSWIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-12
|
19KB
|
703 lines
/* Copyright (C) 1992, 1993 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* gp_mswin.c */
/*
* Microsoft Windows 3.n platform support for Ghostscript.
* Original version by Russell Lang and Maurice Castro with help from
* Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
* initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
*/
#include "windows_.h"
#if WINVER >= 0x030a
#include <shellapi.h>
#endif
#include "stdio_.h"
#include <stdlib.h>
#include <stdarg.h>
#include "ctype_.h"
#include "dos_.h"
#include <io.h>
#include "malloc_.h"
#include "gp_mswin.h"
#include "gp_mswtx.h"
#include "memory_.h"
#include "gx.h"
#include "gp.h"
#include "gpcheck.h"
#include "gserrors.h"
#include "gxdevice.h"
#include <fcntl.h>
#include <signal.h>
#include "string_.h"
/* for imitation pipes */
#include "stream.h"
#include "filedev.h" /* must come after stream.h */
extern stream *gs_stream_stdin; /* from zfiledev.c */
extern stream *gs_stream_stdout; /* from zfiledev.c */
extern stream *gs_stream_stderr; /* from zfiledev.c */
/* Library routines not declared in a standard header */
extern char *getenv(P1(const char *));
/* Imported from gsmain.c */
extern void gs_exit(P1(int exit_status));
/* Imported from gp_msdos.c */
int gp_file_is_console(P1(FILE *));
/* ------ from gnuplot winmain.c plus new stuff ------ */
/* limits */
#define MAXSTR 255
/* public handles */
HWND hwndtext;
HINSTANCE phInstance;
const LPSTR szAppName = "Ghostscript";
const LPSTR szImgName = "Ghostscript Image";
char FAR win_prntmp[MAXSTR]; /* filename of PRN temporary file */
int win_init = 0; /* flag to know if gp_exit has been called */
int win_exit_status;
BOOL is_win31 = FALSE;
/* gsview.exe */
BOOL gsview = FALSE;
HWND gsview_hwnd = NULL;
BOOL gsview_next = FALSE;
LPSTR gsview_option = "-sGSVIEW=";
/* redirected stdio */
TW textwin; /* text window structure */
/* imitation pipes */
HGLOBAL pipe_hglobal = NULL;
LPBYTE pipe_lpbyte = NULL;
UINT pipe_count = 0;
BOOL CALLBACK _export AbortProc(HDC, int);
int main(int argc, char *argv[], char *env[]);
/* our exit handler */
/* also called from Text Window WM_CLOSE */
void win_exit(void)
{
#if WINVER >= 0x030a
/* disable Drag Drop */
if (is_win31)
DragAcceptFiles(hwndtext, FALSE);
#endif
/* if we didn't exit through gs_exit() then do so now */
if (win_init)
gs_exit(0);
fcloseall();
if (win_exit_status) {
/* display message box so error messages in hwndtext can be read */
char buf[20];
sprintf(buf, "Exit code %d",win_exit_status);
MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
}
/* tell gsview that we are closing */
if (gsview)
SendMessage(gsview_hwnd, WM_GSVIEW, GSWIN_CLOSE, (LPARAM)NULL);
TextClose(&textwin);
}
int PASCAL
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
{
int i;
#if defined(_MSC_VER) /* MSC doesn't give us _argc and _argv[] so ... */
#define MAXCMDTOKENS 128
int _argc=0;
LPSTR _argv[MAXCMDTOKENS];
_argv[_argc] = "gswin.exe";
_argv[++_argc] = _fstrtok( lpszCmdLine, " ");
while (_argv[_argc] != NULL)
_argv[++_argc] = _fstrtok( NULL, " ");
#endif
is_win31 = FALSE;
{
WORD version = LOWORD(GetVersion());
if (((LOBYTE(version)<<8) | HIBYTE(version)) >= 0x30a)
is_win31 = TRUE;
}
if (hPrevInstance) {
MessageBox((HWND)NULL,"Can't run twice", szAppName, MB_ICONHAND | MB_OK);
return FALSE;
}
/* copy the hInstance into a variable so it can be used */
phInstance = hInstance;
/* start up the text window */
textwin.hInstance = hInstance;
textwin.hPrevInstance = hPrevInstance;
textwin.nCmdShow = cmdShow;
textwin.Title = szAppName;
textwin.hIcon = LoadIcon(hInstance, "texticon");
textwin.DragPre = "(";
textwin.DragPost = ") run\r";
textwin.ScreenSize.x = 80;
textwin.ScreenSize.y = 80;
textwin.KeyBufSize = 2048;
textwin.CursorFlag = 1; /* scroll to cursor after \n & \r */
textwin.shutdown = win_exit;
if (TextInit(&textwin))
exit(1);
hwndtext = textwin.hWndText;
(void) atexit((atexit_t)win_exit); /* setup exit handler */
/* check if we are to use gsview.exe */
for (i=0; i<_argc; i++) {
if (!strncmp(_argv[i], gsview_option, strlen(gsview_option))) {
gsview_hwnd = (HWND)atoi(_argv[i]+strlen(gsview_option));
if (gsview_hwnd != (HWND)NULL) {
if (!IsWindow(gsview_hwnd)) {
char buf[80];
sprintf(buf,"GSVIEW window handle %u is invalid",(int)gsview_hwnd);
MessageBox(hwndtext, buf, szAppName, MB_OK);
return 0;
}
gsview = TRUE;
/* give gsview the handle to our text window */
SendMessage(gsview_hwnd, WM_GSVIEW, HWND_TEXT, (LPARAM)hwndtext);
}
}
}
main(_argc, _argv, environ);
/* never reached */
win_exit();
return 0;
}
BOOL CALLBACK _export
AbortProc(HDC hdcPrn, int code)
{
(void)gp_check_interrupts();
return(TRUE);
}
/* ------ Process message loop ------ */
/*
* Check messages and interrupts; return true if interrupted.
* This is called frequently - it must be quick!
*/
int
gp_check_interrupts(void)
{
TextMessage();
return 0;
}
/* ====== Generic platform procedures ====== */
/* ------ Initialization/termination (from gp_itbc.c) ------ */
/* Do platform-dependent initialization. */
void
gp_init(void)
{
win_init = 1;
}
/* Do platform-dependent cleanup. */
void
gp_exit(int exit_status, int code)
{
win_init = 0;
win_exit_status = exit_status;
}
/* ------ Printer accessing ------ */
/* Forward references */
private int gp_printfile(P1(char *));
/* Open a connection to a printer. A null file name means use the */
/* standard printer connected to the machine, if any. */
/* Return NULL if the connection could not be opened. */
FILE *
gp_open_printer(char *fname, int binary_mode)
{ if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
{ FILE *pfile;
pfile = gp_open_scratch_file(gp_scratch_file_name_prefix,
win_prntmp, "wb");
return pfile;
}
else
return fopen(fname, (binary_mode ? "wb" : "w"));
}
/* Close the connection to the printer. */
void
gp_close_printer(FILE *pfile, const char *fname)
{
fclose(pfile);
if (strlen(fname) && strcmp(fname,"PRN"))
return; /* a file, not a printer */
gp_printfile(win_prntmp);
unlink(win_prntmp);
}
/* Windows does not provide API's in the SDK for writing directly to a */
/* printer. Instead you are supposed to use the Windows printer drivers. */
/* Ghostscript has its own printer drivers, so we need to use some API's */
/* that are documented only in the Device Driver Adaptation Guide */
/* that comes with the DDK. Prototypes taken from DDK <print.h> */
DECLARE_HANDLE(HPJOB);
HPJOB WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
int WINAPI StartSpoolPage(HPJOB);
int WINAPI EndSpoolPage(HPJOB);
int WINAPI WriteSpool(HPJOB, LPSTR, int);
int WINAPI CloseJob(HPJOB);
int WINAPI DeleteJob(HPJOB, int);
int WINAPI WriteDialog(HPJOB, LPSTR, int);
int WINAPI DeleteSpoolPage(HPJOB);
HWND hDlgModeless;
/* Modeless dialog box - Cancel printing */
BOOL CALLBACK _export
CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)